package com.gxa.saas.portal.controller.luoj;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.saas.core.common.JsonResult;
import com.gxa.saas.core.entity.dto.luoj.TbAddressDTO;
import com.gxa.saas.core.entity.vo.luoj.UserVO;
import com.gxa.saas.portal.service.AddressService;
import com.gxa.saas.portal.thread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
@Api(value = "地址")
public class AddressController {

    @Resource
    private AddressService addressService;

    @Resource
    private UserThreadLocal userThreadLocal;

    @ApiOperation("查看全部地址")
    @GetMapping("/queryAllAddress")
    public Map queryAllAddress() throws JsonProcessingException {
        String value = String.valueOf(userThreadLocal.get());
        ObjectMapper objectMapper = new ObjectMapper();
        UserVO userVO = objectMapper.readValue(value, UserVO.class);
        List<TbAddressDTO> tbAddressDTOS = addressService.queryAllAddress(userVO.getId());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(tbAddressDTOS);
        jsonResult.setMsg("查询成功！");
        return jsonResult.getMap();
    }

    @ApiOperation("设置和添加地址（同一个接口）")
    @PostMapping("/updateAddressStatus")
    public Map updateAddressStatus(@RequestBody TbAddressDTO addressDTO){
        Boolean aBoolean = addressService.updateAddressStatus(addressDTO);
        JsonResult jsonResult = new JsonResult();
        if(aBoolean) {
            jsonResult.setCode("200");
            jsonResult.setMsg("设置成功！");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("设置失败！");
        }
        return jsonResult.getMap();
    }

    @ApiOperation("编辑地址前（查看某一个地址）")
    @GetMapping("/getAddressById")
    public Map getAddressById(Integer addressId){
        TbAddressDTO addressById = addressService.getAddressById(addressId);
        JsonResult jsonResult = new JsonResult();
        if(addressById!=null) {
            jsonResult.setCode("200");
            jsonResult.setData(addressById);
            jsonResult.setMsg("查看成功！");
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("查看失败！");
        }
        return jsonResult.getMap();
    }

//    @ApiOperation("保存编辑地址")
//    @PostMapping("/saveAddress")
//    public Map saveAddress(@RequestBody TbAddressDTO addressDTO){
//
//        JsonResult jsonResult = new JsonResult();
////        if(addressById!=null) {
////            jsonResult.setCode("200");
////            jsonResult.setData(addressById);
////            jsonResult.setMsg("查看成功！");
////        }else{
////            jsonResult.setCode("0");
////            jsonResult.setMsg("查看失败！");
////        }
//        return jsonResult.getMap();
//    }
}
